package software

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/hibiken/asynq"
	"log"
	"time"
)

// address = "127.0.0.1:11052"
// db = 19
// pass = "root"
// idleTimeout = 600

func main(ctx context.Context) {
	redisConfig := g.Config().MustGet(ctx, "redis.default").MapStrStr() // address db pass

	client := asynq.NewClient(asynq.RedisClientOpt{
		Addr: redisConfig["address"],
	})
	defer func(client *asynq.Client) {
		err := client.Close()
		if err != nil {
			panic(err)
		}
	}(client)

	task, err := NewEmailDeliveryTask(42, "some:template:id")
	if err != nil {
		log.Fatalf("could not create task: %v", err)
	}

	info, err := client.Enqueue(task) // ProcessIn|MaxRetry|Timeout|Queue|Deadline|Unique
	info, err = client.Enqueue(task, asynq.ProcessIn(24*time.Hour))
	info, err = client.Enqueue(task, asynq.MaxRetry(10), asynq.Timeout(3*time.Minute))
	if err != nil {
		log.Fatalf("could not enqueue task: %v", err)
	}
	fmt.Print(info)
}
func main2(ctx context.Context) {
	redisConfig := g.Config().MustGet(ctx, "redis.default").MapStrStr() // address db pass

	mux := asynq.NewServeMux()
	// mux.HandleFunc(tasks.TypeEmailDelivery, tasks.HandleEmailDeliveryTask)
	// mux.Handle(tasks.TypeImageResize, tasks.NewImageProcessor())

	srv := asynq.NewServer(
		asynq.RedisClientOpt{
			Addr: redisConfig["address"],
		},
		asynq.Config{
			Concurrency: 10,
			Queues: map[string]int{
				"critical": 6,
				"default":  3,
				"low":      1,
			},
		},
	)

	if err := srv.Run(mux); err != nil {
		log.Fatalf("could not run backend: %v", err)
	}
}
